package file.handling;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import engine.Edge;
import engine.Vertex;
import gui.Frame;

public class OpenFile
{
   private ArrayList<Edge> edges = new ArrayList<Edge>();
   File file;
   Frame frame;
   private ArrayList<Vertex> vertices = new ArrayList<Vertex>();

   public OpenFile(File file, Frame frame)
   {
      this.file = file;
      this.frame = frame;
      try
      {
         readFile();
      }
      catch (SAXParseException e)
      {
         // TODO Auto-generated catch block

      }
   }

   public Vertex getVertex(String id)
   {
      for (int i = 0; i < vertices.size(); i++)
         if (vertices.get(i).getuID().equals(id))
            return vertices.get(i);
      return null;
   }

   public void readFile() throws SAXParseException
   {
      int status=0;
      int w =0;
      try
      {
         Document d = XMLUtil.read(file);
         Element root = d.getDocumentElement();

         NodeList vertex = root.getElementsByTagName("Vertex");
         int numVert = vertex.getLength();
         frame.getView().clear();
         for (int i = 0; i < numVert; i++)
         {
            Element e = (Element) vertex.item(i);
            int x = Integer.parseInt(e.getAttribute("x"));
            int y = Integer.parseInt(e.getAttribute("y"));
            try
            {
               status = Integer.parseInt(e.getAttribute("vstatus"));
            }
            catch (NumberFormatException nf)
            {
               status=0;
            }
            if (!frame.getView().isVertexTooClose(x, y))
               vertices.add(new Vertex(x, y, e.getAttribute("name"),status));
         }

         NodeList n = root.getElementsByTagName("Edge");
         int numEdge = n.getLength();
         for (int i = 0; i < numEdge; i++)
         {
            Element e = (Element) n.item(i);
            Vertex v1 = getVertex(e.getAttribute("id1"));
            Vertex v2 = getVertex(e.getAttribute("id2"));
            try
            {
               w = Integer.parseInt(e.getAttribute("weight"));
            }
            catch(NumberFormatException nf)
            {
               w=10;
            }
            try
            {
               status = Integer.parseInt(e.getAttribute("estatus"));
            }
            catch (NumberFormatException nf)
            {
               status=0;
            }
            // if (e.getAttribute("color").equals("green"))
            // c = Color.green;
            // else if (e.getAttribute("color").equals("red"))
            // c = Color.red;
            if (v1 != null && v2 != null)
            {
               Edge edge = new Edge(v1, v2, w);
               edge.setStatus(status);
               v1.addEdge(edge);
               v2.addEdge(edge);
               edges.add(edge);
            }
         }

         frame.getView().setVertices(vertices);
         frame.getView().setEdges(edges);
         frame.getView().repaint();
      }
      catch (IOException e)
      {
         // TODO: handle exception
      }
      catch (SAXException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

}
